package com.ax.center.controller;

import com.ax.center.annotation.Logging;
import com.ax.center.dto.ApiResponse;
import com.ax.center.entity.Announcement;
import com.ax.center.enumeration.ExportFormat;
import com.ax.center.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zy
 * @date 2023/7/10 13:46
 */
@RestController
@RequestMapping(value = "/announcementController")
@Api(tags = "公告服务")
@CrossOrigin(origins = "http://localhost:5173")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping(value = "/无效的请求")
    public void  test(@RequestBody Announcement announcement){return;}


        @PostMapping(value = "/createAnnouncement")
        @ApiOperation("创建公告")
        @Logging(description = "创建公告")
        public ApiResponse createAnnouncement(
                @ApiParam(value = "公告实体")
                @RequestBody Announcement announcement) {
            return announcementService.createAnnouncement(announcement);
        }

        @GetMapping("/getAnnouncementById/{id}")
        @ApiOperation("根据公告id查看具体信息")
        @Logging(description = "根据公告id查看具体信息")
        public ApiResponse getAnnouncementById(
                @ApiParam("公告id")
                @PathVariable("id") Long id) {
            return announcementService.getAnnouncementById(id);
        }

    @PostMapping("/updateAnnouncement")
            @ApiOperation("更新公告")
            @Logging(description = "更新公告")
            public ApiResponse updateAnnouncement(
                    @ApiParam("公告实体")
                    @RequestBody Announcement announcement) {
                return announcementService.updateAnnouncement(announcement);
        }

        @GetMapping("/deleteAnnouncement/{id}")
        @ApiOperation("删除公告")
        @Logging(description = "删除公告")
        public ApiResponse deleteAnnouncement(
                @ApiParam("公告id")
                @PathVariable("id") Long id) {
            return announcementService.deleteAnnouncement(id);
        }

        @GetMapping("/search")
        @ApiOperation("查询公告")
        @Logging(description = "查询公告")
        public ApiResponse searchAnnouncements(
                @ApiParam("关键字")
                @RequestParam(value = "keyword", required = false) String keyword,
                                               @RequestParam(value = "publisher", required = false) String publisher,
                                               @RequestParam(value = "startTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
                                               @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
            return announcementService.searchAnnouncements(keyword, publisher, startTime, endTime);
        }

        @ApiOperation("导出公告")
        @GetMapping("/export")
        public ResponseEntity<ByteArrayResource> exportAnnouncements(@RequestParam(value = "format") ExportFormat format) throws IOException {
            List<Announcement> announcements = announcementService.list();
            ByteArrayResource resource = announcementService.exportAnnouncements(announcements, format);
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=announcements." + format.getExtension());
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentLength(resource.contentLength())
                    .body(resource);
        }
    }


